package com.hll.dao;

import com.hll.model.User;
import common.JDBCAbstractCallBack;
import common.JDBCTemplate;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    /**
     * 使用模块方法
     */
    JDBCTemplate<User> jdbcTemplate = new JDBCTemplate<User>();

    public User getUserByName(final String userName) {

        if (userName == null || userName.equals("")) {
            return null;
        }
        return jdbcTemplate.queryOne("SELECT * FROM user WHERE user_name = ?", new JDBCAbstractCallBack<User>() {
            @Override
            public User rsToObject(ResultSet rs) throws SQLException {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("password"));
                return user;
            }

            @Override
            public void setParams(PreparedStatement stmt) throws SQLException {
                stmt.setString(1, userName);
            }
        });

    }

}
